import h5py
import pandas as pd

# coingecko_file_path = "/remote/iosg/data-2/buckets/feed.derived.interval_h5/coin/main/PT1M/Spot.Coingecko.v3/%s/Spot.Coingecko.v3--market_analysis.h5"
coingecko_file_path = "/remote/iosg/data-2/buckets/feed.derived.interval_h5/coin/main/P1DT/Spot.Coingecko.v3/%s/Spot.Coingecko.v3--market_analysis.h5"

def read_h5file(file_path):
  
  with h5py.File(file_path, 'r') as h5file:
    universe = h5file['universe'][:].astype(str)
    timestamps = h5file['timestamp'][:].astype(int)
    h5_topic = list(h5file.keys())
    dfs = []
    df_topic = []
    for topic in h5_topic:
      if topic not in ('universe', 'timestamp'):
        df_topic.append(topic)
        df = pd.DataFrame(h5file[topic][:], columns=universe, index=timestamps)
        dfs.append(df)
  return pd.concat(dfs, keys=df_topic, axis=1).swaplevel(0, 1, 1)

def get_coingecko_df(date):
  file_path = coingecko_file_path % date
  df = read_h5file(file_path)
  return df
